import { createStore } from 'vuex'
import persist from 'vuex-persistedstate'
const adressFil = (val,state) => {
   return state.addressList.filter(item => item.address == val).length
}
export default createStore({
  state: {
    addressList:[], // 所有的行程数据
  },
  getters: {
    list(state){
      // let arr = [{name:'昌平',val:0},{name:'西安',val:0},{name:'海淀',val:0},{name:'低风险',val:0}];
      // state.addressList.forEach(item => {
      //    if (item.address == '无'){
      //      arr[3].val++;
      //    } else {
      //      let i = arr.findIndex(n => n.name == item.address);
      //      arr[i].val++;
      //    }
      // })
      // return arr
      return [
        {
          name: '昌平',
          val: adressFil('昌平',state)
        },
        {
          name: '西安',
          val: adressFil('西安',state)
        },
        {
          name: '海淀',
          val: adressFil('海淀',state)
        },
        {
          name: '低风险',
          val: adressFil('无',state)
        }
      ]
    }
  },
  mutations: {
    save(state,val){ // 保存行程记录
      //  console.log(val);
      // val 是一个对象，他是引用数据类型的数。
      let obj = {
        ...val,
        time: new Date().toLocaleDateString()
      }
      // console.log(val);
      state.addressList.push(obj);
    },
    del(state,i){ // 删除的函数
      state.addressList.splice(i,1);
    }
  },
  actions: {
  },
  modules: {
  },
  plugins: [
    new persist({
      storage: window.localStorage,
    }),
  ]
})
